<?php

namespace App\Http\Controllers\Home;

use App\Model\Role;
use App\Model\Workflow;
use App\Model\Workflow_group;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/*
 *审批流程管理
 */
class ApprovalController extends CommonController
{
    //

	protected $workflow;
	protected $company_id;
	protected $workflow_group;
	protected $Role;
	public function __construct()
	{
		parent::__construct();
		$this->workflow=new Workflow();
		$this->workflow_group=new Workflow_group();
		$this->Role=new Role();
	}

	public function add_approval(Request $request){

//		模拟数据开始
//		$data= [
//			"name" => "中止审批3",
//			  "level" => "4",
//			  "type" => "2",
//				"style"=>"2",
//			  "one" => "1",
//			  "two" => "2",
//			  "three" => "3",
//			  "four" => "4",
//			  "status" => "1",
//			  "group" => [1,2,3],
//			];
//		模拟数据结束

		$data=$request->input();
//		return $data;

		DB::beginTransaction();
		try{
			$userData=Auth::user();
//			$data=$request->only($this->workflow->getFillable());
			$data['create_name']=$userData->realname;
			$data['create_uid']=$userData->id;
			$data['company_id']=$this->company_id;
			$this->update_other($data);
			$result=$this->workflow->create($data);
			$groupData=[];
			foreach ($data['group'] as $kk=>$vv){
				$groupData[$kk]['workflow_id']=$result->id;
				$groupData[$kk]['group_id']=$vv;
				$groupData[$kk]['created_at']=Carbon::now();
			}
			DB::table('workflow_group')->insert($groupData);

			DB::commit();

		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}

	public function update_other($data){
		$this->workflow->where(['company_id'=>$data['company_id'],'type'=>$data['type']])
			->update(['status'=>0]);
	}

	public function list_approval(Request $request){
		$where=[];
		$where['company_id']=$this->company_id;
		$where['name']=isset($request->name)?$request->name:null;
		$where['status']=isset($request->status)?$request->status:null;
		try{
			$data=$this->workflow->lists($where);
			foreach($data as $kk=>$vv){
				if($data[$kk]['style']==1){
					$data[$kk]['style']='排班';
				}
				if($data[$kk]['style']==2){
					$data[$kk]['style']='中止';
				}
				if($data[$kk]['style']==3){
					$data[$kk]['style']='调动';
				}
					if($data[$kk]['style']==4){
					$data[$kk]['style']='终止';
				}
//				$data[$kk]['statu']=$vv['status']==1?'有效':'失效';
//				$data[$kk]['create_name']=$vv['user_info']['realname'];
			}

		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}


	public function edit(Request $request){
		$id=$request->id;
		$data=$this->workflow->where(['id'=>$id])->first();
//		查询该流程所有的业务组
		$groups=$this->workflow_group->where(['workflow_id'=>$id])->pluck('group_id');
		$data['groups']=$groups;
		return $this->success($data);

	}

	public function edit_approval(Request $request){
		$group=$request->group;
		$userData=Auth::user();
		$data=$request->only($this->workflow->getFillable());
		$data['create_name']=$userData->realname;
		$data['create_uid']=$userData->id;
		DB::beginTransaction();
		try{
			$result=$this->workflow->where(['id'=>$request->id])->update($data);
			$this->workflow_group->where(['workflow_id'=>$request->id])->delete();
			$groupData=[];
			foreach ($group as $kk=>$vv){
				$groupData[$kk]['workflow_id']=$request->id;
				$groupData[$kk]['group_id']=$vv;
				$groupData[$kk]['created_at']=Carbon::now();
			}
			DB::table('workflow_group')->insert($groupData);
			DB::commit();

		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}


	public function del_approval(Request $request){
		$style=$this->workflow->where(['id'=>$request->id])->first()->style;
		try{
			if($request->status==1){
				$data=$this->workflow->where(['company_id'=>$style,'style'=>$style])->update(['status'=>0]);
			}
			$data=$this->workflow->where(['id'=>$request->id])->update(['status'=>$request->status]);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}


//	审批角色列表
	public function roles(Request $request){
		$where['display_name']=isset($request->display_name)?$request->display_name:null;
		$where['company_id']=$this->company_id;
		$where['status']=null;
		$data=$this->Role->all_role($where);
		return $this->success($data);
	}

//	审批类型
	public function approvel_list(){
		$data=[
			['id'=>1,'name'=>'中止'],
			['id'=>2,'name'=>'排班'],
			['id'=>3,'name'=>'调动'],
			['id'=>4,'name'=>'终止'],
		];
		return $this->success($data);
	}
}
